package cn.stylefeng.guns.core.common.util;

import cn.stylefeng.guns.modular.system.entity.Car;
import cn.stylefeng.guns.modular.system.entity.QuestionAnswer;
import cn.stylefeng.guns.modular.system.entity.User;
import cn.stylefeng.guns.modular.system.model.AllRentDto;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.fastjson.JSON;
import org.apache.commons.beanutils.BeanMap;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ObjectUtil {
    public static List<Map<String, Object>> compareTwoClass(Object class1, Object class2) throws ClassNotFoundException, IllegalAccessException {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        //获取对象的class
        Class<?> clazz1 = class1.getClass();
        Class<?> clazz2 = class2.getClass();
        //获取对象的属性列表
        Field[] field1 = clazz1.getDeclaredFields();
        Field[] field2 = clazz2.getDeclaredFields();
        //遍历属性列表field1
        for (int i = 0; i < field1.length; i++) {
            //遍历属性列表field2
            for (int j = 0; j < field2.length; j++) {
                //如果field1[i]属性名与field2[j]属性名内容相同
                if (field1[i].getName().equals(field2[j].getName())) {
                    field1[i].setAccessible(true);
                    field2[j].setAccessible(true);
                    //如果field1[i]属性值与field2[j]属性值内容不相同
                    if (!compareTwo(field1[i].get(class1), field2[j].get(class2))) {
                        Map<String, Object> map2 = new HashMap<String, Object>();
                        field1[i].getName();
                        map2.put("name", field1[i].getName());
                        map2.put("old", field1[i].get(class1));
                        map2.put("new", field2[j].get(class2));
                        list.add(map2);
                    }
                    break;
                }
            }
        }

        return list;
    }

    /**
     * 对比两个数据是否内容相同
     * @param object1
     * @param object2
     * @return
     */
    public static boolean compareTwo(Object object1, Object object2) {

        if (object1 == null && object2 == null) {
            return true;
        }
        //以下注掉代码，看具体需求。因有时会出现源数据是没有进行赋值，因此是null；而通过EditText获取值的时候，虽然没有值，但是会变成""，但是本质是没有赋值的。
        if (object1 == "" && object2 == null) {
            return true;
        }
        if (object1 == null && object2 == "") {
            return true;
        }
        if (object1 == null && object2 != null) {
            return false;
        }
        if (object1.equals(object2)) {
            return true;
        }
        return false;
    }

    public static List<Map<String, Object>> toListMap(String json){
        List<Object> list = JSON.parseArray(json);

        List< Map<String,Object>> listw = new ArrayList<Map<String,Object>>();
        for (Object object : list){
            Map<String,Object> ageMap = new HashMap<String,Object>();
            Map <String,Object> ret = (Map<String, Object>) object;
            //取出list里面的值转为map
        for (Map.Entry<String, Object> entry : ret.entrySet()) {
                ageMap.put(entry.getKey(),entry.getValue());
                System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
                listw.add(ageMap);  //添加到list集合  成为 list<map<String,Object>> 集合
            }
            listw.add(ret);
        }
        return listw;

    }


    public static String toSplit(String str){
        StringBuilder sb = new StringBuilder();
        String[] s = str.trim().split(" ");
        for (String s1 : s) {
            sb.append(s1 + ",");
        }
        return sb.toString();
    }

    public static List<Map<String, Object>> getListNoteToObject(List<Map<String, Object>> rents, Car car) throws IllegalAccessException {
        //便利rents取值
        List<Map<String, Object>> maps = new ArrayList<>();
        for (Map<String, Object> rent : rents) {
            AllRentDto allRentDto = new AllRentDto();
            allRentDto.setUserId((Long) rent.get("userId"));
            if (ToolUtil.isEmpty(rent.get("rentAssess"))){
                allRentDto.setRentAssess("这个用户太懒,没有发表任何评论");
            }else {
                allRentDto.setRentAssess(rent.get("rentAssess").toString());
            }
            allRentDto.setCarType(car.getCarType());
            allRentDto.setUsername(rent.get("cardName").toString());
            Map<String, Object> map = new HashMap<String, Object>();
            Field[] declaredFields = allRentDto.getClass().getDeclaredFields();
            for (Field field : declaredFields) {
                field.setAccessible(true);
                map.put(field.getName(), field.get(allRentDto));
            }
            boolean add = maps.add(map);
        }
        return maps;
    }

    public static QuestionAnswer replaceObject(QuestionAnswer questionAnswer, QuestionAnswer now) {
        now.setAnswerId(questionAnswer.getAnswerId());
        now.setUserId(questionAnswer.getQuestionId());
        now.setUsername(questionAnswer.getQuestionName());
        now.setQuestion(questionAnswer.getAnswer());
        now.setAnswer(null);
        now.setQuestionName(questionAnswer.getUsername());
        now.setQuestionId(questionAnswer.getQuestionId());
        return now;
    }


    /**
     * 用于主页车辆推荐
     * @param list
     * @return
     */
    public static List<Car> recommendCars(List<Car> list) {
        List<Car> cars = new ArrayList<>();
        if (!ToolUtil.isEmpty(list)){
            if (list.size() != 0){
                if (list.size() <= 6){
                    return list;
                }
                for (int i = 0 ; i < 6 ; i++){
                    cars.add(list.get(i));
                }
            }
            return cars;
        }
        return cars;

    }
}
